import {defHttp} from '/@/utils/http/axios';
import { useMessage } from "/@/hooks/web/useMessage";
import { ref, computed, unref } from 'vue';
import TaskAllocationModal from './components/TaskAllocationModal.vue';

const { createConfirm } = useMessage();

enum Api {
  list = '/customers_pool/customersPool/list',
  save= '/customers_pool/customersPool/add',
  edit= '/customers_pool/customersPool/edit',
  deleteOne = '/customers_pool/customersPool/delete',
  deleteBatch = '/customers_pool/customersPool/deleteBatch',
  importExcel = '/customers_pool/customersPool/importExcel',
  exportXls = '/customers_pool/customersPool/exportXls',
  taskAllocation = '/customers_pool/customersPool/taskAllocation',
  contactsList = '/customers_pool/customersPool/listContactsByMainId',
  contactsSave= '/customers_pool/customersPool/addContacts',
  contactsEdit= '/customers_pool/customersPool/editContacts',
  contactsDelete = '/customers_pool/customersPool/deleteContacts',
  contactsDeleteBatch = '/customers_pool/customersPool/deleteBatchContacts',
  customerPrincipalList = '/customers_pool/customersPool/listCustomerPrincipalByMainId',
  customerPrincipalSave= '/customers_pool/customersPool/addCustomerPrincipal',
  customerPrincipalEdit= '/customers_pool/customersPool/editCustomerPrincipal',
  customerPrincipalDelete = '/customers_pool/customersPool/deleteCustomerPrincipal',
  customerPrincipalDeleteBatch = '/customers_pool/customersPool/deleteBatchCustomerPrincipal',
  getRequirement = '/customers_pool/customersPool/getRequirement',

  getBusinessDeptUsers = '/customers_pool/customersPool/getBusinessDeptUsers',
  assignBusinessDeptUser = '/customers_pool/customersPool/assignBusinessDeptUser',
  assignBusinessDeptUser2 = '/customers_pool/customersPool/assignBusinessDeptUser2',
  deleteDeveloper = '/developers/developers/delete',
}
/**
 * 导出api
 * @param params
 */
export const getExportUrl = Api.exportXls;

/**
 * 任务分配接口
 * @param params
 */
export const taskAllocation = (params) =>
  defHttp.post({ url: Api.taskAllocation, params });

/**
 * 导入api
 */
export const getImportUrl = Api.importExcel;

/**
 * 列表接口
 * @param params
 */
export const list = (params) =>
  defHttp.get({ url: Api.list, params });

/**
 * 删除单个
 */
export const deleteOne = (params,handleSuccess) => {
  return defHttp.delete({ url: Api.deleteOne, params }, { joinParamsToUrl: true }).then(() => {
    handleSuccess();
  });
}

/**
 * 删除单个需求跟进人员
 */
export const deleteDeveloper = (params,handleSuccess) => {
  return defHttp.delete({ url: Api.deleteDeveloper, params }, { joinParamsToUrl: true }).then(() => {
    handleSuccess();
  });
}
/**
 * 批量删除
 * @param params
 */
export const batchDelete = (params, handleSuccess) => {
  createConfirm({
    iconType: 'warning',
    title: '确认删除',
    content: '是否删除选中数据',
    okText: '确认',
    cancelText: '取消',
    onOk: () => {
      return defHttp.delete({ url: Api.deleteBatch, data: params }, { joinParamsToUrl: true }).then(() => {
        handleSuccess();
      });
    }
  });
}

/**
 * 保存或者更新
 * @param params
 */
export const saveOrUpdate = (params, isUpdate) => {
  let url = isUpdate ? Api.edit : Api.save;
  return defHttp.post({ url: url, params },{ isTransformResponse: false });
}
  
/**
 * 列表接口
 * @param params
 */
export const contactsList = (params) => {
  if(params['customerNumber']){
    params['customerId'] = params['customerNumber'];
    delete params['customerNumber'];
    return defHttp.get({ url: Api.contactsList, params });
  }
  return Promise.resolve({});
}

/**
 * 删除单个
 */
export const contactsDelete = (params,handleSuccess) => {
  
  return defHttp.delete({ url: Api.contactsDelete, params }, { joinParamsToUrl: true }).then(() => {
    handleSuccess();
  });
}

/**
 * 批量删除
 * @param params
 */
export const contactsDeleteBatch = (params, handleSuccess) => {
  createConfirm({
    iconType: 'warning',
    title: '确认删除',
    content: '是否删除选中数据',
    okText: '确认',
    cancelText: '取消',
    onOk: () => {
      return defHttp.delete({ url: Api.contactsDeleteBatch, data: params }, { joinParamsToUrl: true }).then(() => {
        handleSuccess();
      });
    }
  });
}

/**
 * 保存或者更新
 * @param params
 */
export const  contactsSaveOrUpdate = (params, isUpdate) => {
  // 只有当customerId为空且customerNumber存在时才进行赋值
  if (!params['customerId'] && params['customerNumber']) {
    params['customerId'] = params['customerNumber'];
  }

  let url = isUpdate ? Api.contactsEdit : Api.contactsSave;
  return defHttp.post({ url: url,params },{ isTransformResponse: false });
}

/**
 * 导入
 */
export const contactsImportUrl = '/customers_pool/customersPool/importContacts'

/**
 * 导出
 */
export const contactsExportXlsUrl = '/customers_pool/customersPool/exportContacts'
  
/**
 * 列表接口
 * @param params
 */
export const customerPrincipalList = (params) => {
  if(params['id']){
    return defHttp.get({ url: Api.customerPrincipalList, params });
  }
  return Promise.resolve({});
}

/**
 * 删除单个
 */
export const customerPrincipalDelete = (params,handleSuccess) => {
  return defHttp.delete({ url: Api.customerPrincipalDelete, params }, { joinParamsToUrl: true }).then(() => {
    handleSuccess();
  });
}

/**
 * 批量删除
 * @param params
 */
export const customerPrincipalDeleteBatch = (params, handleSuccess) => {
  createConfirm({
    iconType: 'warning',
    title: '确认删除',
    content: '是否删除选中数据',
    okText: '确认',
    cancelText: '取消',
    onOk: () => {
      return defHttp.delete({ url: Api.customerPrincipalDeleteBatch, data: params }, { joinParamsToUrl: true }).then(() => {
        handleSuccess();
      });
    }
  });
}

/**
 * 保存或者更新
 * @param params
 */
export const  customerPrincipalSaveOrUpdate = (params, isUpdate) => {
  let url = isUpdate ? Api.customerPrincipalEdit : Api.customerPrincipalSave;
  return defHttp.post({ url: url, params },{ isTransformResponse: false });
}

/**
 * 导入
 */
export const customerPrincipalImportUrl = '/customers_pool/customersPool/importCustomerPrincipal'

/**
 * 导出
 */
export const customerPrincipalExportXlsUrl = '/customers_pool/customersPool/exportCustomerPrincipal'

export const taskList = (params) =>
  defHttp.get({ url: '/customers_pool/customersPool/taskAllocation' });

export const task2sysUser = (params) =>
  defHttp.post({ url: '/customers_pool/customersPool/taskAllocation2sysUser', params }, { 
    joinParamsToUrl: true,
    errorMessageMode: 'message', // 恢复全局错误处理
    isTransformResponse: false // 确保返回原始响应
  });
  
/**
 * 获取需求列表
 * @param params
 */
export const getRequirementList = (params) => {
  if (params['customerId']) {
    return defHttp.get({ url: Api.getRequirement, params });
  }
  return Promise.resolve({});
};


/**
 * 获取商务部门用户列表
 * @param params
 */
export const getBusinessDeptUsers = () =>
  defHttp.get({ url: Api.getBusinessDeptUsers }, { isTransformResponse: false });

/**
 * 分配商务部门用户
 * @param params
 */
export const assignBusinessDeptUser = (params) =>
  defHttp.post({ url: Api.assignBusinessDeptUser, params }, {
    joinParamsToUrl: true,
    errorMessageMode: 'message',
    isTransformResponse: false
  });


  /**
 * 添加开发人员
 * @param params
 */
export const assignBusinessDeptUser2 = (params) =>
  defHttp.post({ url: Api.assignBusinessDeptUser2, params }, {
    joinParamsToUrl: true,
    errorMessageMode: 'message',
    isTransformResponse: false
  });
